package org.itboys.core.controller.front.member;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.dto.member.SundayMemberDTO;
import org.itboys.core.service.mongo.front.SundayFrontMemberService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/11/26
 * @Description：用户
 */
@Controller
@RequestMapping("sunday/mobile/member")
@Api(value = "member", description = "用户")
public class SundayFrontMemberController {

    @Autowired
    private SundayFrontMemberService memberService;

    @RequestMapping(value = "/findOne", method = {RequestMethod.GET})
    @ApiOperation(value = "【租赁端】【供应商】查找单个")
    public void findOne(
            HttpServletRequest request, HttpServletResponse response
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundayMemberDTO result = memberService.findOne(memberId);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】【供应商】登录")
    public void loginWithThird(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "登录端口 1：租赁端  3：供应商", required = true) @RequestParam(value = "userType") Integer userType,
            @ApiParam(value = "登录类型 0:手机号, 1:微信, 2:QQ, 3:支付宝", required = true) @RequestParam(value = "thirdType") Integer thirdType,
            @ApiParam(value = "unionId") @RequestParam(value = "unionId", required = false) String unionId,
            @ApiParam(value = "昵称") @RequestParam(value = "nickName", required = false) String nickName,
            @ApiParam(value = "头像") @RequestParam(value = "image", required = false) String image,
            @ApiParam(value = "手机号") @RequestParam(value = "mobile", required = false) String mobile,
            @ApiParam(value = "密码,供应商端用户登录时传入") @RequestParam(value = "password", required = false) String password,
            @ApiParam(value = "验证码") @RequestParam(value = "code", required = false) String code
    ) {
        SundayMemberDTO result = memberService.login(userType, thirdType, unionId, nickName, image, mobile, password, code);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】修改个人信息")
    public void changeCompany(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "公司名称") @RequestParam(value = "company", required = false) String company,
            @ApiParam(value = "公司执照") @RequestParam(value = "permit", required = false) String permit,
            @ApiParam(value = "头像") @RequestParam(value = "image", required = false) String image,
            @ApiParam(value = "手机号:用户绑定手机号") @RequestParam(value = "mobile", required = false) String mobile,
            @ApiParam(value = "验证码") @RequestParam(value = "code", required = false) String code
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundayMemberDTO result = memberService.update(memberId, company, permit, image, mobile, code);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/forgetPWD", method = {RequestMethod.POST})
    @ApiOperation(value = "【供销商】忘记密码（未登录）")
    public void forgetPWD(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "手机号") @RequestParam(value = "mobile", required = false) String mobile,
            @ApiParam(value = "验证码") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "新密码") @RequestParam(value = "newPassword", required = false) String newPassword
    ) {
        memberService.forgetPWD(mobile, code, newPassword);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/updatePWD", method = {RequestMethod.POST})
    @ApiOperation(value = "【供销商】用户修改密码(登录状态)")
    public void updatePWD(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(value = "验证码") @RequestParam(value = "code", required = true) String code,
            @ApiParam(value = "旧密码") @RequestParam(value = "oldPassword", required = true) String oldPassword,
            @ApiParam(value = "新密码") @RequestParam(value = "newPassword", required = true) String newPassword
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        memberService.updatePWD(memberId, mobile, code, oldPassword, newPassword);
        ResponseMessage.success();
    }
}
